package com.kehao.leetcode.jzoffer;

import org.junit.Test;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/7/7 15:52
 */
public class JZSolution052 {

    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    TreeNode result;
    TreeNode p;
    public TreeNode increasingBST(TreeNode root) {
        dfs(root);
        return result;
    }

    private void dfs(TreeNode root) {
        if(root==null) return;
        dfs(root.left);
        if(result == null){
            result = root;
            p = result;
        }else {
            p.left = null;
            p.right = root;
            p = p.right;
        }
        p.left = null;
        dfs(root.right);
    }

    @Test
    public void test01(){
        buildTree(new LinkedList<>(Arrays.asList(new Integer[]{})));
    }

    private TreeNode buildTree(List<Integer> list){
        if(list.size()==0) return null;
        Integer remove = list.remove(0);
        if(remove==null) return null;
        TreeNode result = new TreeNode(remove);
        result.left = buildTree(list);
        result.right = buildTree(list);
        return result;
    }


}
